package com.chiancloud.shield.core.conf;

import com.chiancloud.shield.core.persist.ConfTreeSerDeser;
import com.chiancloud.shield.core.persist.PersistKeys;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wanghui on 14-8-20.
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public final class ConfTree {


    public static final int INITAL_MAP_CAPACITY = 3;

    protected static final Logger
            log = LoggerFactory.getLogger(ConfTree.class);


    public String schema = PersistKeys.SCHEMA;

    public Map<String, Object> metadata = new HashMap<>(INITAL_MAP_CAPACITY);

    public Map<String, String> global =
            new HashMap<>(INITAL_MAP_CAPACITY);

    public Map<String, Map<String, String>> components =
            new HashMap<>(INITAL_MAP_CAPACITY);

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        try {
            return toJson();
        } catch (Exception e) {
            log.warn("Failed to convert to JSON ", e);
            return super.toString();
        }
    }

    public String toJson() throws IOException,
            JsonGenerationException,
            JsonMappingException {
        return ConfTreeSerDeser.toString(this);
    }

}
